#include "alarmalerttone.h"
#include "ui_alarmalerttone.h"
#include"maininterface.h"
Q_GLOBAL_STATIC(AlarmAlertTone,theInstance);
AlarmAlertTone::AlarmAlertTone(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::AlarmAlertTone)
{
    ui->setupUi(this);

    initAlarmAlertTone();
    this->setWindowTitle("报警音效设置");
    this->setWindowIcon(QIcon(":res/res/AlarmSound.ico"));
    connect(MainInterface::instance(),&MainInterface::sendAlarmTipSound,this,&AlarmAlertTone::playAlarmSound);
    // 连接信号槽
    connect(player, &QMediaPlayer::stateChanged, [=](QMediaPlayer::State state) {
        if (state == QMediaPlayer::PlayingState) {
            stopTimer->start(1000); // 播放开始时启动1秒定时器
        }
    });
    connect(stopTimer, &QTimer::timeout, player, &QMediaPlayer::stop);


  }

AlarmAlertTone *AlarmAlertTone::instance()
{
return theInstance;
}

AlarmAlertTone::~AlarmAlertTone()
{
    delete ui;
}

void AlarmAlertTone::initAlarmAlertTone()
{

   QString soundSourcePath=soundPath+soundSet[0];
   qDebug()<<soundSourcePath;
   player=new QMediaPlayer(this);
   player->setMedia(QUrl::fromLocalFile(soundSourcePath));
   stopTimer = new QTimer(this);
   stopTimer->setSingleShot(true);//设定时器单次触发
   testPlayer=new QMediaPlayer(this);
   ui->spinBoxAlarmVolume->setRange(0,100);

}

void AlarmAlertTone::switchPlayAlarm(int index,QString &soundSourcePath)
{

    soundSourcePath=soundPath+soundSet[index];
}

unsigned int& AlarmAlertTone::setVolume(unsigned int &volume)
{
 volume=ui->spinBoxAlarmVolume->value();
 ui->spinBoxAlarmVolume->setValue(volume);
 return volume;

}



void AlarmAlertTone::on_pushButtonConfirm_clicked()
{
//获取当前的警报声
int currentAlarmIndex=ui->comboBoxAlarmSet->currentIndex();
QString soundSourcePath=soundPath+soundSet[currentAlarmIndex];
player->setMedia(QUrl::fromLocalFile(soundSourcePath));
setVolume(volume);
player->setVolume(setVolume(volume));
QMessageBox::information(this,"success","该变警报声成功！");

}


void AlarmAlertTone::on_pushButtonStop_clicked()
{
testPlayer->stop();
}


void AlarmAlertTone::on_pushButtonPlay_clicked()
{
 QString soundSourcePath;
 int index=ui->comboBoxAlarmSet->currentIndex();
 switchPlayAlarm(index,soundSourcePath);
 testPlayer->setMedia(QUrl::fromLocalFile(soundSourcePath));
 testPlayer->setVolume(setVolume(volume));
 testPlayer->play();
}


void AlarmAlertTone::on_pushButtonExit_clicked()
{

    this->close();
}

void AlarmAlertTone::playAlarmSound()
{
player->play();
}

